package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.testng.annotations.DataProvider;

import com.example.fw.ApplicationManager;
import com.example.obj.ContactObject;

public class ContactDataGenerator{
	
	static Random rnd = new Random();
	static String months[] = {"January", "February", "March", "April",
			"May", "June", "July", "August", 
			"September", "October", "November", "December"};
		
	@DataProvider(name = "randomContacts")
	public static Iterator<Object[]> generateRandomContacts() {
		List<Object[]> list = new ArrayList<Object[]>();
		List<ContactObject> contacts = new ContactDataGenerator().generateRandomContactList(5);
		for (ContactObject contact : contacts) {
			list.add(new Object[]{contact, true});
		}
		ContactObject emptyContact = new ContactObject();
		list.add(new Object[]{emptyContact, false});
		return list.iterator();
	}	
	
	@DataProvider(name = "contactsFromFile")
	public static Iterator<Object[]> loadContactsFromFile() throws IOException {
		List<Object[]> list = new ArrayList<Object[]>();
		BufferedReader reader = new BufferedReader(new FileReader(ApplicationManager.getInstance().getProperty("contactsFile","contacts.dat")));
		String line = reader.readLine();
		while (line != null){
			String[] parts = line.split("\t");
			ContactObject contact = new ContactObject()
				.setFirstName(parts[0])
				.setLastName(parts[1])
				.setAddress(parts[2])
				.setPhoneHome(parts[3])
				.setPhoneMobile(parts[4])
				.setPhoneWork(parts[5])
				.setEmail(parts[6])
				.setEmail2(parts[7])
				.setBday(parts[8])
				.setBmonth(parts[9])				
				.setByear(parts[10])
				.setGroupName(parts[11])						
				.setAddress2(parts[12])
				.setPhone2(parts[13]);			
			list.add(new Object[]{contact, true});
			line = reader.readLine();
		}
		return list.iterator();
	}		
	
	public static void main(String[] args) throws IOException{
		if (args.length < 2) {
			System.out.println("Specify two parameters: file and count");
			return;
		}
		new ContactDataGenerator().generateDataToFile(args[0], Integer.parseInt(args[1]));		
	}
	
	private void generateDataToFile(String fileName, int count) throws IOException {		
		File file = new File(fileName);
		if (file.exists()){
			System.out.println ("File exists, stop generator");
			return;
		}		
		FileWriter writer = new FileWriter(file);
		List<ContactObject> contacts = generateRandomContactList(count);
		for (ContactObject contact : contacts) {
			writer.write(contact.firstName +"\t" + contact.lastName + "\t" + contact.address + "\t" + 
					contact.phoneHome + "\t" + contact.phoneMobile + "\t" + contact.phoneWork + "\t" + 
					contact.email + "\t" + contact.email2 + "\t" + contact.bday + "\t" + 
					contact.bmonth + "\t" + contact.byear + "\t" + contact.groupName + "\t" + 
					contact.address2 + "\t" + contact.phone2 + "\n");
		}
		writer.close();		
		System.out.println ("Contacts were generated");
	}

	private List<ContactObject> generateRandomContactList(int count) {
		List<ContactObject> list = new ArrayList<ContactObject>();		
		for (int i = 0; i < count; i++) {
			ContactObject contact = new ContactObject()
				.setFirstName("FirstName" + rnd.nextInt())
				.setLastName("LastName" + rnd.nextInt())
				.setAddress("Address" + rnd.nextInt())
				.setPhoneHome("" + rnd.nextInt(9999999))
				.setPhoneMobile("89" + rnd.nextInt(999999999))
				.setPhoneWork("" + rnd.nextInt(9999999))
				.setEmail("mail" + rnd.nextInt() + "@domain.ru")
				.setEmail2("mail" + rnd.nextInt() + "@domain.ru")
				.setBday("" + (1 + rnd.nextInt(27)))
				.setBmonth(months[rnd.nextInt(11)])				
				.setByear("" + (1900 + rnd.nextInt(150)))
				.setGroupName("[none]")						
				.setAddress2("Address" + rnd.nextInt())
				.setPhone2("" +  + rnd.nextInt(9999999));
			list.add(contact);
		}
		return list;
	}
}
